package com.rockbb.common.simpleapp;

import java.util.concurrent.ThreadFactory;

public class ExecutorFactory implements ThreadFactory {

	// 线程名称前缀
	private String prefix;
	// 线程编号, 与前缀一起组合成为线程名称, 如果不指定, 默认以 0 开始
	private int consequence;

	/**
	 * 创建线程工厂, 默认的线程名称开始于 executor-0
	 */
	public ExecutorFactory() {
		this("executor");
	}

	/**
	 * 创建线程工厂, 如果输入的值为prefix, 则的线程名称开始于 prefix-0
	 * 
	 * @param prefix
	 */
	public ExecutorFactory(String prefix) {
		this(prefix, 0);
	}

	/**
	 * 创建线程工厂, 指定线程名称前缀和线程起始编号, 如果输入值为prefix和3, 则线程名称开始于 prefix-3
	 * 
	 * @param prefix
	 * @param init
	 */
	public ExecutorFactory(String prefix, int init) {
		this.prefix = prefix;
		consequence = init;
	}

	public Thread newThread(Runnable r) {
		Thread thread = new Thread(r);
		thread.setName(prefix +"-"+ consequence);
		consequence ++;
		return thread;
	}
}
